package cn.ubest.mvvmcore.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.simple.SimpleComponent;

import cn.ubest.mvvmcore.R;
import cn.ubest.mvvmcore.utils.ResourcesUtils;


/**
 * Content: 刷新头部布局
 * Create by wk on 2019/8/20
 */
public class CustomRefreshHeader extends SimpleComponent implements RefreshHeader {

    private static final int DELAY_TIME = 500;

    private ImageView ivIcon;
    private TextView tvText;
    private ProgressBar pbLoading;

    public CustomRefreshHeader(Context context) {
        this(context, null);
    }

    protected CustomRefreshHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    protected CustomRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View view = LayoutInflater.from(context).inflate(R.layout.layout_refresh_header, null);
        ivIcon = view.findViewById(R.id.iv_icon);
        tvText = view.findViewById(R.id.tv_text);
        pbLoading = view.findViewById(R.id.pb_loading);

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ResourcesUtils.getDimens(R.dimen.dp_40));
//        setLayoutParams(layoutParams);
//        setGravity(CENTER);
        addView(view, layoutParams);
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
//        if (success) {
//            tvText.setText(getResources().getString(R.string.refresh_success));
//        } else {
//            tvText.setText(getResources().getString(R.string.refresh_fail));
//        }
        tvText.setText(getResources().getString(R.string.refresh_finished));
        stopLoading();
        return DELAY_TIME;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState,
                               @NonNull RefreshState newState) {
        switch (newState) {
            case PullDownToRefresh: // 下拉过程
                tvText.setText(getResources().getString(R.string.refresh_pull_down));
                ivIcon.setImageResource(R.drawable.icon_down_refresh);
                ivIcon.setVisibility(VISIBLE);
                break;
            case ReleaseToRefresh: // 松开刷新
                tvText.setText(getResources().getString(R.string.refresh_release_to));
                ivIcon.setImageResource(R.drawable.icon_up_refresh);
                ivIcon.setVisibility(VISIBLE);
                break;
            case Refreshing: // loading中
                tvText.setText(getResources().getString(R.string.refreshing));
                startLoading();
                break;
//            case RefreshFinish:
//                break;
//            case PullDownCanceled:
//                break;
//            case None:
//                break;
            default:
                break;
        }
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
//        tvText.setText(getResources().getString(R.string.refreshing));
//        startLoading();
    }

    public void startLoading() {
        ivIcon.setVisibility(GONE);
        pbLoading.setVisibility(VISIBLE);
    }

    public void stopLoading() {
        ivIcon.setVisibility(GONE);
        pbLoading.setVisibility(GONE);
    }
}
